/**
 * @file models/replay.js
 * @author
 */
import { routerRedux } from 'dva/router';
import api from '../api';
import moment from 'moment'


export default {
	namespace: 'replayDetail',
	state: {
		filterValue:'',
		// begindate:moment().format('YYYY-MM-DD'),
		// enddate:moment().format('YYYY-MM-DD'),
		begindate:'',
		enddate:'',
		tradeArr:[]
	},
	subscriptions : {
		setup({dispatch, history}) {
			history.listen(({ pathname, query }) => {
				// 没有参数时，向后端请求数据，并且改变query
				if( pathname === '/tradeReplay/investReplayDetail' ){
					dispatch({type: 'whetherRequest',payload:query,pathname})
				}
			})
		}
	},
	effects : {

		* whetherRequest({
				payload,pathname
			}, { put,select}) {


				const {
					filterValue,
					begindate,
					enddate,
					currentpage,
					pagesize,
					sortField,
					sortOrder
				}=payload

				if(filterValue||begindate||enddate){
					if(!(begindate&&enddate)){
						yield put({
							type:'changeRouter',
							payload:{
								pathname
							}
						})
					}else{
						yield put({
							type:'setState',
							payload
						})
						yield put({
							type:'query',
							payload
						})
					}
				}else{
					yield [
						put({
							type:'query',
							payload
						}),
						put({
							type:'setState',
							payload:{
								filterValue:'1'
							}
						})
					]
				}
		},
		* queryDetail({payload}, {call, put,select}){

			// console.log(111);

			const [closeOrder,commentClosed,kline] = yield [
				call(api.getCloseOrder, {
					trader:payload.trader,
					orderId:payload.orderId
				}),
				call(api.getCommentClosedPos, {
					trader:payload.trader,
					closeOrderId:payload.closeOrderId,
					openOrderId:payload.openOrderId
				}),
				call(payload.to.diff(payload.from, 'days')>5?api.getSimpleDayKline:api.getSimpleMinKline, {
					stockCode:payload.stockCode,
					from:payload.to.diff(payload.from, 'days')>5?payload.from.subtract(30, 'days').format('YYYYMMDD'):payload.from.format('YYYYMMDD093000'),
					to:payload.to.diff(payload.from, 'days')>5?payload.to.add(30, 'days').format('YYYYMMDD'):payload.to.format('YYYYMMDD150000')
				})
			]

			yield put({
				type: 'queryDetailSuccess',
				payload:{
					...payload,
					closeOrder:closeOrder.data,
					commentClosed:commentClosed.data,
					kline:kline.data
				}
			});

		},

		* query({payload}, {call, put,select}){
			const state=yield select(state => state.replayDetail)

			const [
				shares,
				trades,
				openPosition,
			] = yield [
				call(api.replay.getReplayShares, {
					trader:payload.trader,
					begindate:payload.begindate||state.begindate,
					enddate:payload.enddate||state.begindate
				}),
				call(api.getClosedPositions, {
					trader:payload.trader,
					begindate:payload.begindate||state.begindate,
					enddate:payload.enddate||state.begindate,
					currentpage:payload.currentpage||1,
					pagesize:payload.pagesize||10,
					sortField:payload.sortField||null,
					sortOrder:payload.sortOrder||null
				}),
				call(api.getOpenPositions, {
					trader:payload.trader
				}),
			]

			yield put({type:'clearState'})

			yield put({
				type: 'querySuccess',
				payload:{
					info:shares.data.trader_detail,
					shares:shares.data.stock_detail,
					loss:shares.data.stock_pnl_stat.loss,
					income:shares.data.stock_pnl_stat.profit,
					trades:trades,
					openPosition,
				}
			});

		},

		* changeRouter({payload}, {put}){
			yield put(routerRedux.push(payload))
		}

	},
	reducers : {
		setState(state, {payload}) {
			return {
				...state,
				...payload
			}
		},

		clearState(state) {
      return{
				...state,
				info:null,
				shares:null,
				trades:null,
				openPosition:null,
				account:null
			}
    },
		querySuccess(state, {payload}) {
			return {
				...state,
				...payload
			}
		},
		queryDetailSuccess(state, {payload}) {

			state.tradeArr.push(payload)
			return {
				...state
			}
		}



	}
};
